package net.sxnic.ugr.dept;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import net.sxinfo.core.entity.AbstractEntity;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.User;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 
 * @Introduction：组织机构实体，项目申报系统中的Dept是无属性的机构，即此实体只是记录了组织的结构，不记录是组织单位还是非组织单位
 * @author 孙宇飞
 * @Date: 2014-1-3
 */
@Entity
@Table(name = "ugr_dept")
@BatchSize(size = 10)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Dept extends AbstractEntity{

	private static final long serialVersionUID = -1259832680891001883L;

	//机构编码
	@Column(nullable=false)
	private String code;
	//机构名称
	private String name;
	//机构描述
	private String desp;
	//机构全称
	private String fullName;
	//机构排序
	private long deptOrder;	
	//机构类别（000省 010各市 020省直单位 030驻并机构 040大型企业  050区县  060工作单位  080厅下属单位  090申报单位或个人 999管理员）
	protected String ctype;

	@ManyToOne(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Dept parent;
	
	@ManyToMany(fetch=FetchType.LAZY)
	@OrderBy("code")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Dept> child;

	@ManyToMany(fetch = FetchType.LAZY)
	@javax.persistence.OrderBy("userOrder")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<User> users;

	public Dept() {
		super();
		users = new LinkedHashSet<User>(); 
		child = new LinkedHashSet<Dept>();
	}

	public Dept(String name) {
		super();
		this.name = name;
		users = new HashSet<User>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesp() {
		return desp;
	}

	public void setDesp(String desp) {
		this.desp = desp;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCtype() {
		return ctype;
	}

	public void setCtype(String ctype) {
		this.ctype = ctype;
	}

	public Dept getParent() {
		return parent;
	}

	public void setParent(Dept parent) {
		this.parent = parent;
	}

	public Set<Dept> getChild() {
		return child;
	}

	public void setChild(Set<Dept> child) {
		this.child = child;
	}

	public long getDeptOrder() {
		return deptOrder;
	}

	public void setDeptOrder(long deptOrder) {
		this.deptOrder = deptOrder;
	}
	
	/**
	 * 添加子部门
	 * @param child 该部门的子
	 */
	public void addChild(Dept child){
		this.getChild().add(child);
	}
	
	/**
	 * 移除子部门
	 * @param child
	 */
	public void removeChild(Dept child){
		if(this.getChild().contains(child)){
			this.getChild().remove(child);
		}
	}
	
	/**
	 * 获取节点类表
	 * @return 从父节点到自身
	 */
	public List<Dept> getNodeList() {
		//注意这里使用的数据类型，因为后面要用到addFirst(node)方法
		LinkedList<Dept> list = new LinkedList<Dept>();
		Dept node = this;
		while (node != null) {
			//在 LinkedList 的开头处添加指定的新节点。
			list.addFirst(node);
			//获取节点的父
			node = node.getParent();
		}
		return list;
	}

	/**
	 * 获取节点列表的ID
	 * @return 从父到自身的ID数组
	 */
	public String[] getNodeIds() {
		List<Dept> depts = getNodeList();
		String[] ids = new String[depts.size()];
		int i = 0;
		for (Dept c : depts) {
			ids[i++] = c.getId();
		}
		return ids;
	}

	/**
	 * 获取层级（主要用于展示）
	 * @return 第一层为0，第二层为1，以此类推
	 */
	public int getDeep() {
		int deep = 0;
		Dept parent = getParent();
		while (parent != null) {
			deep++;
			parent = parent.getParent();
		}
		return deep;
	}
	
	/**
	 * 获取父的ID
	 * @return 父ID 
	 */
	public String getParentId() {
		Dept parent = getParent();
		if (parent != null) {
			return parent.getId();
		} else {
			return null;
		}
	}

}
